<?php

namespace Admin\Controller;
use Think\Controller;

header("Content-type: text/html; charset=utf-8");
class IndexController extends Controller {
    public function index(){
       $this->display();
    }
    public function _initialize()
    {
        if(!session('?admin')){
            $this->error('没有权限','../Login/login');
            exit;
        }
    }
   
    public function usersList(){
        $User = M('Users'); // 实例化User对象
        $count = $User->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        

        $list = $User->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        /*$this->assign('users',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出*/
        
        $this->users=$list;
        $this->page=$show;
        
        $this->display(); // 输出模板
    }
     public function groupList(){
        $User = M('groups'); // 实例化User对象
        $count = $User->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $where['groupname']=array('like','%'.$_POST['name'].'%');
        $list = $User->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

  
        
        $this->groups=$list;
        $this->page=$show;
        
       

        $this->display(); // 输出模板
    }
    
    public function activityList(){
        $User = M('activity'); // 实例化User对象
        $count = $User->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $where['name']=array('like','%'.$_POST['name'].'%');
        $list = $User->join('groups ON group_id = groups.id')->order('activity.aid desc')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();

       
        
        $this->groups=$list;
        $this->page=$show;
        
        $this->display(); // 输出模板
    }
    public function findGroups()
    {
        $group= M('groups'); 
        $groups=$group->select();

        $this->findGroups=$groups;

        
        $this->display('Index/activityAdd');

    }

    public function newsList(){
        $User = M('news'); // 实例化User对象
       
        $where['title']=array('like','%'.$_POST['name'].'%');
        $list = $User->join('activity ON activity_id =activity.aid')->where($where)->order('id desc')->select();

        $this->groups=$list;
        $this->page=$show;
        $this->display(); // 输出模板
    }
   public function activity1Add(){
    $activityModel=M('activity');
    $activityModel->create();
    
    $num=$_POST['leader'];
    $userModel=M('users');
    if($userModel->where("num=$num")->select()){
    $activityModel->hit=0;

    var_dump($activityModel);

    $activitya=$activityModel->add();

    if($activitya){
       $this->redirect('activityList');
    }else{

        $this-error('no');
    }
}else{
    $this->error('此学号不存在');
}
   }

   public function groupModify($id){
   
        $usersmodel=M('groups');
        $aa=$usersmodel->where("id=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
    public function userModify($id){
   
        $usersmodel=M('users');
        $aa=$usersmodel->where("id=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
    public function activityModify($id){

        $group= M('groups'); 
        $groups=$group->select();

        $this->findGroups=$groups;
   
        $usersmodel=M('activity');
        $aa=$usersmodel->where("aid=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
   public function newsModify($id){
   
        $usersmodel=M('news');
        $aa=$usersmodel->where("id=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
   public function memberModify($id){
   
        $usersmodel=M('member');
        $aa=$usersmodel->where("mid=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
   public function stupracModify($id){
   
        $usersmodel=M('stuprac');
        $aa=$usersmodel->where("sid=$id")->select();
        $this->aa=$aa;
        $this->display();

  
   }
   public function groupsUpdate(){
        $user=M("groups");
        $user->create();
        
        if($_FILE['logo']['error']==0){
            $photoinfo=$this->upload();
            $user->logo=$photoinfo['logo']['savename'];
        }else{
             $user->logo=$_POST['photo'];
        }
        echo "logo:".$_POST['photo'];
        if ($user->save()) {
                 $this->success('修改成功','groupList',4);
        } else{
                
                $this->error("修改失败");
        }
    }
    public function newsUpdate(){
        $user=M("news");
        $user->create();
        
        if($_FILE['pic']['error']==0){
            $photoinfo=$this->upload();
            $user->pic=$photoinfo['pic']['savename'];
        }else{
             $user->pic=cat.jpg;
        }
       
        if ($user->save()) {
                 $this->success('修改成功','newsList',4);
        } else{
                
                $this->error("修改失败");
        }
    }
    public function activityUpdate(){
        

        $user=M("activity");
        $user->create();
        
       
        if ($user->save()) {
                 $this->success('修改成功','activityList',4);
        } else{
                
                $this->error("修改失败");
        }
    }
 public function userUpdate(){
        

        $user=M("users");
        $user->create();
        
        if($_FILE['photo']['error']==0){
            $photoinfo=$this->upload();
            $user->photo=$photoinfo['photo']['savename'];
        }else{
             
        }
      
        if ($user->save()) {
                 $this->success('修改成功','usersList',4);
        } else{
                
                $this->error("修改失败");
        }
    }

   public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/uploads/photo/'; // 设置附件上传根目录
       
        $upload->autoSub=false;
        $info= $upload->upload();  // 上传文件 
         if(!$info) {
               $this->error($upload->getError()); 
        }
        else{ 
              return $info;    
          }                                     

    }
   public  function  groupDel($id){
     $user=M("groups");
     $uu=$user->where("id=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }
   public  function  userDel($id){
     $user=M("users");
     $uu=$user->where("id=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }
   public  function  newsDel($id){
     $user=M("news");
     $uu=$user->where("id=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }
   public  function  groupsMemberDel($id){
     $user=M("members");
     $uu=$user->where("mid=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }
    public  function  activityMemberDel($id){
     $user=M("stuprac");
     $uu=$user->where("sid=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }

   public  function  activityDel($id){
     $user=M("activity");
     $uu=$user->where("aid=$id")->delete();
     if($uu){
        $this->success("删除成功");
     }else{
        $this->error("删除失败");
     }
   }
   public function groupsAdd(){
    $user=M('groups');
    $user->create();
    $num=$_POST['leaders'];
    $userModel=M('users');
    if($userModel->where("num=$num")->select()){
         $user->createtime=date('Y-m-d');
    $user->state=1;
    if($_FILE['logo']['error']==0){
            $photoinfo=$this->upload();
            $user->logo=$photoinfo['logo']['savename'];
        }else{
            $user->logo='cat.jpg';
        }
    if($user->add()){
         $this->success("添加成功");
    }else{
        $this->error("添加失败");
     }
 }else{
    $this->error('此学号不存在');
 }
   
   }
   public function groupsMemberList($id){
      $User = M('members'); // 实例化User对象
    
    $list = $User->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("groups_id=$id")->select();

        $this->groups=$list;
        $this->page=$show;
        $this->groups_id=$id;
        $this->display(); // 输出模板
   }

    public function groupsActivityList($id){
      $User = M('activity'); // 实例化User对象
    
    $list = $User->join("groups ON group_id =groups.id")->where("group_id=$id")->select();

        $this->groups=$list;
       
        $this->groups_id=$id;
        $this->display(); // 输出模板
   }
    public function groupsMemberAdd(){
        $user_realname=$_POST['realname'];
        $user_num=$_POST['num'];

        $userModel=M('Users');

        
        $condition['realname'] = $_POST['realname'];
        $condition['num']= $_POST['num'];
        
        $users=$userModel->where($condition)->select();
        
        echo $users[0]['realname'];
        if($users){
            $users_id=$users[0]['id'];
            $groups_id=(int)$_POST['groups_id'];

            $memberModel=M('members');
            $memberModel->addtime=date('Y-m-d');
            $memberModel->users_id= $users_id;
            $memberModel->groups_id= $groups_id;

            
            if($memberModel->add()){
                $this->success('添加成功');
            }else{
                $this->error('用户不存在');
            }
        }else{
             $this->error('用户不存在');
        }
    }
   public function userAdd(){
    $user=M('users');
    $user->create();
    $user->pass=md5($_POST['pass']);
    $user->num=date("YmdHis").rand(100,999);
    $user->regtime=date('Ymd');
    $user->state=0;
    if($_FILE['photo']['error']==0){
            $photoinfo=$this->upload();
            $user->photo=$photoinfo['photo']['savename'];
        }else{
            $user->photo='cat.jpg';
        }
    if($user->add()){
         $this->success("添加成功");
    }else{
        $this->error("添加失败");
     }
   }
public function seboss($id)
{
    $usersmodel=M('Users');
    $usersmodel->id=$id;
    $usersmodel->state=1;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function isboss($id)
{
    $usersmodel=M('Users');
    $usersmodel->id=$id;
    $usersmodel->state=0;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function success1($id)
{
    $usersmodel=M('groups');
    $usersmodel->id=$id;
    $usersmodel->state=1;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function error1($id)
{
    $usersmodel=M('groups');
    $usersmodel->id=$id;
    $usersmodel->state=0;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function merror($id)
{
    $usersmodel=M('members');
    $usersmodel->mid=$id;
    $usersmodel->state1=1;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function msuccess($id)
{
    $usersmodel=M('members');
    $usersmodel->mid=$id;
    $usersmodel->state1=2;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function merror1($id)
{
    $usersmodel=M('stuprac');
    $usersmodel->sid=$id;
    $usersmodel->state2=1;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function msuccess1($id)
{
    $usersmodel=M('stuprac');
    $usersmodel->sid=$id;
    $usersmodel->state2=2;
    if($usersmodel->save()){
         $this->success("设置成功");
    }else{
        $this->error("添加失败");
     }
}
public function groupsInfo($id){
     $User = M('groups'); // 实例化User对象
     $list = $User->where("id=$id")->select();

    
    

    $User1 = M('members'); // 实例化User对象

    $list1 = $User1->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("groups_id=$id")->select();


    $User2 = M('activity'); // 实例化User对象
    $list2 = $User2->where("group_id=$id")->select();

        $User3 = M('stuprac'); // 实例化User对象
    
        $list3 = $User3->join("activity ON activity_id =activity.aid")->join("users ON users_id =users.id")->select();

        $this->groups=$list;
        $this->users=$list1;
        $this->activity=$list2;
        $this->stuprac=  $list3;
        $this->display(); // 输出模板
}

 public function activityMemberList($id){
         $User = M('stuprac'); // 实例化User对象
    
        $list = $User->join("activity ON activity_id =activity.aid")->join("users ON users_id =users.id")->where("activity_id=$id")->select();

       
       
        $this->activity_id=$id;
         $this->stuprac=$list;
        $this->display(); // 输出模板
 }

 /*public function text(){
 $User = M('stuprac'); // 实例化User对象
    
    $list = $User->join("activity ON activity_id =activity.aid")->join("users ON users_id =users.id")->where("activity_id=2")->select();
    var_dump($list);
        if($list){
          #  $this->success('添加成功');
        }else{
                $this->error('用户不存在');
            }

 }*/
 public function activityMemberAdd(){
        $user_realname=$_POST['realname'];
        $user_num=$_POST['num'];

        $userModel=M('Users');

        
        $condition['realname'] = $_POST['realname'];
        $condition['num']= $_POST['num'];
        
        $users=$userModel->where($condition)->select();
        
        echo $users[0]['realname'];
        if($users){
            $users_id=$users[0]['id'];
            $activity_id=(int)$_POST['activity_id'];

            $memberModel=M('stuprac');
            $memberModel->time=date('Y-m-d');
            $memberModel->state2=2;
            $memberModel->users_id= $users_id;
            $memberModel->activity_id= $activity_id;

            
            if($memberModel->add()){
                $this->success('添加成功');
            }else{
                $this->error('用户不存在');
            }
        }else{
             $this->error('用户不存在');
        }
    }
    public function findActivity(){
        $group= M('activity'); 
        $groups=$group->select();

        $this->findActivity=$groups;

        
        $this->display('Index/newsAdd'); 
    }

    public function newsAdd1(){
    $activityModel=M('news');
    $activityModel->create();

    if($_FILE['pic']['error']==0){
            $photoinfo=$this->upload();
            $activityModel->pic=$photoinfo['pic']['savename'];
        }else{
            $activityModel->pic='cat.jpg';
        }

    var_dump($activityModel);

    $activitya=$activityModel->add();

    if($activitya){
       $this->redirect('newsList');
    }else{

        $this-error('no');
    }
   }

    public function passwordModify1(){
        $user = M('Admin'); 
        $pass1=md5($_POST['pass1']);
        $pass2=md5($_POST['pass2']);
        $pass3=md5($_POST['pass3']);
        $user_id=$_POST['id'];
        if(!$pass2=$pass3){
            $this->error('密码不一致');
        }else{
            $users=$user->where("id=$user_id")->select();
            if($users){
                $pass=$users[0]['pass'];
                if($pass==$pass1){
                    $user->pass=$pass2;
                    $user->id=$user_id;
                    if($user->save()){
                        $this->success('修改成功','../Index/index');
                    }else{
                        echo "$pass2";
                        var_dump($users);
                         #$this->error('修改失败');
                    }

                }else{
                  $this->error('旧密码错误');
                }
            }else{
                 $this->error('超找失败');
            }
    }
}
public function userSearch(){
    $userForm=M('users'); 
    $count = $userForm->count();// 查询满足要求的总记录数
    $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
    $show = $Page->show();// 分页显示输出

    $where['username']=array('like','%'.$_POST['username'].'%');

   
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        

        $list = $userForm->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        /*$this->assign('users',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出*/
        
        $this->users=$list;
        $this->page=$show;

        
   
        $this->display('usersList');
    }
}